//
//  HWTopicsMainListVC.m
//  HappyWeekend
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWTopicsMainListVC.h"
#import "HWTopicsSpecialTableViewCell.h"
#import "HWTopicsHouseTableViewCell.h"
#import "HWGoOutTableViewCell.h"
#import "HWTopicsBannerModel.h"
#import "HWChannelsModel.h"
#import "HWTopicsMoreViewController.h"
#import <MJRefresh.h>
@interface HWTopicsMainListVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *BannerArray;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *outArray;
@end

@implementation HWTopicsMainListVC
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor cyanColor];
    [self addTableView];
    [self setUpBannerData];
    [self setData];
   
    // Do any additional setup after loading the view.
}

- (void)setUpBannerData
{
    [HWRequestManager requestWithUrl:@"http://api.guozhoumoapp.com/v2/collections?limit=10&offset=0" parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *collectionsArray = dataDic[@"collections"];
        self.BannerArray = [NSMutableArray array];
        for (NSDictionary*oneDic in collectionsArray) {
            HWTopicsBannerModel *model = [[HWTopicsBannerModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_BannerArray addObject:model];
        }
        [_tableView reloadData];
        
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}
//专题合集下面的数据
- (void)setData
{
    [HWRequestManager requestWithUrl:@"http://api.guozhoumoapp.com/v2/channel_groups/all" parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *channel_goupsArray = dataDic[@"channel_groups"];
        NSDictionary *fistchanneDic = channel_goupsArray.firstObject;
        NSArray *channeArray = fistchanneDic[@"channels"];
        self.dataArray = [NSMutableArray array];
        self.outArray = [NSMutableArray array];
        for (NSDictionary*oneDic in channeArray) {
            HWChannelsModel *model = [[HWChannelsModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_dataArray addObject:model];
        }
        NSDictionary*towChannelDic = channel_goupsArray.lastObject;
        NSArray *channelsArray = towChannelDic[@"channels"];
        for (NSDictionary*towDic in channelsArray) {
            HWChannelsModel *model = [[HWChannelsModel alloc]init];
            [model setValuesForKeysWithDictionary:towDic];
            [_outArray addObject:model];
        }
        
        [_tableView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
}

//添加tableView
- (void)addTableView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight*0.3);
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HWTopicsSpecialTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [_tableView registerClass:[HWTopicsHouseTableViewCell class] forCellReuseIdentifier:@"HouseCell"];
    [_tableView registerClass:[HWGoOutTableViewCell class] forCellReuseIdentifier:@"GoOutCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        HWTopicsSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        tableView.rowHeight = 120;
        tableView.allowsSelection = NO;
        tableView.userInteractionEnabled = YES;
        for (int i = 0; i <4; i++) {
            UIButton *button = [cell.scrollView viewWithTag:100+i];
            [button addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        }
        cell.array = _BannerArray;
        return cell;
    }else if(indexPath.section == 1){
        HWTopicsHouseTableViewCell *houseCell = [tableView dequeueReusableCellWithIdentifier:@"HouseCell"];
        tableView.rowHeight = 170;
        [houseCell.cookButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [houseCell.DIYButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [houseCell.filmButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [houseCell.mettingButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        
        houseCell.Array = _dataArray;
        
        return houseCell;
    }else
    {
        HWGoOutTableViewCell *outCell = [tableView dequeueReusableCellWithIdentifier:@"GoOutCell"];
        tableView.rowHeight = 300;
        [outCell.weekendButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [outCell.experienceButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [outCell.rimButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [outCell.freeButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [outCell.barButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        [outCell.tasteFoodButton addTarget:self action:@selector(cilkButton:) forControlEvents:(UIControlEventTouchUpInside)];
        outCell.outArray = _outArray;
        return outCell;
    }
    
}

//bannerBurtton按钮方法
- (void)cilkButton:(UIButton*)but
{
   
    HWTopicsMoreViewController *moreVC = [[HWTopicsMoreViewController alloc]init];
    if (but.tag > 99) {
        HWTopicsBannerModel *model = _BannerArray[but.tag-100];
        moreVC.model = model;
        moreVC.string = model.title;
        [self.navigationController pushViewController:moreVC animated:YES];
        

        
    }else if(but.tag > 9){
        //HWTopicsMoreViewController *moreVC = [[HWTopicsMoreViewController alloc]init];
        HWChannelsModel *Cmodel = _dataArray[but.tag-10];
        
        moreVC.CModel = Cmodel;
        moreVC.string = Cmodel.name;
        [self.navigationController pushViewController:moreVC animated:YES];
    

    }else if(but.tag <7&&but.tag >0){
        HWChannelsModel *Cmodel = _dataArray[but.tag-1];
        moreVC.CModel = Cmodel;
        moreVC.string = Cmodel.name;
        [self.navigationController pushViewController:moreVC animated:YES];
        

    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
